﻿using SRB.Tg.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices.Filters;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.DrawingStandard
{
    public static class PictureFrameTools
    {
        /// <summary>
        /// 获取桥梁图纸配置中基本配置
        /// </summary>
        /// <param name="dtProjectConfig"></param>
        public static void GetBridgeConfig(System.Data.DataTable dtProjectConfig)
        {
            if (dtProjectConfig.Rows.Count > 0)
            {
                string maptkpath = dtProjectConfig.Rows[0]["map_tk_path"].ToString().Replace("//",@"\");
                double bottomheight = double.Parse(dtProjectConfig.Rows[0]["pf_bottomheight"].ToString());
                double leftwidth = double.Parse(dtProjectConfig.Rows[0]["pf_leftwidth"].ToString());
                double topheight = double.Parse(dtProjectConfig.Rows[0]["pf_topheight"].ToString());
                double rightwidth = double.Parse(dtProjectConfig.Rows[0]["pf_rightwidth"].ToString());
                string selectscalelist = dtProjectConfig.Rows[0]["pf_scalelist"].ToString();
                Extensions.ChangeValues(maptkpath, bottomheight, leftwidth, topheight, rightwidth, selectscalelist);
            }
        }

        /// <summary>
        /// 获取桥梁图纸配置中图框配置
        /// </summary>
        /// <param name="dtProject"></param>
        /// <param name="type"></param>
        /// <param name="dtProjectConfig"></param>
        /// <returns></returns>
        public static DrawingConfigModel GetDrawingConfig(System.Data.DataTable dtProject, string catalogid, System.Data.DataTable dtProjectConfig)
        {
            double font_pf_height = 4.5;
            double font_pf_scale = 0.85;
            string xStringname = "161";
            string yStringname = "12.5";
            string xStringno = "366";
            string yStringno = "12.5";
            double font_location_designer_x = 214;
            double font_location_designer_y = 12.5;
            double font_location_reviewer_x = 242;
            double font_location_reviewer_y = 12.5;
            double font_location_first_reviewer_x = 272;
            double font_location_first_reviewer_y = 12.5;
            double font_location_second_reviewer_x = 302;
            double font_location_second_reviewer_y = 12.5;
            double font_location_third_reviewer_x = 332;
            double font_location_third_reviewer_y = 12.5;
            double font_location_page_x = 387.2;
            double font_location_page_y = 280.5;
            double font_location_page_sum_x = 402.2;
            double font_location_page_sum_y = 280.5;
            DrawingConfigModel drawingConfigModel = new DrawingConfigModel();

            if (dtProject.Rows.Count > 0)
            {
                System.Data.DataRow[] dr = dtProject.Select("id = '" + catalogid + "'");
                drawingConfigModel.drawing_scale =dr[0]["drawing_scale"].ToString().Length>0? int.Parse(dr[0]["drawing_scale"].ToString()):1;
                drawingConfigModel.drawing_no = dr[0]["drawing_no"].ToString();
                drawingConfigModel.name = dr[0]["name"].ToString();
                drawingConfigModel.designer = dr[0]["designer"].ToString();
                drawingConfigModel.reviewer = dr[0]["reviewer"].ToString();
                drawingConfigModel.first_reviewer = dr[0]["first_reviewer"].ToString();
                drawingConfigModel.second_reviewer = dr[0]["second_reviewer"].ToString();
                drawingConfigModel.third_reviewer = dr[0]["third_reviewer"].ToString();
            }
            //如果存在桥梁配置，则读取，不存在则使用默认值
            if (dtProjectConfig.Rows.Count > 0)
            {
                string location_designer = dtProjectConfig.Rows[0]["location_designer"].ToString();
                string location_reviewer = dtProjectConfig.Rows[0]["location_reviewer"].ToString();
                string location_first_reviewer = dtProjectConfig.Rows[0]["location_first_reviewer"].ToString();
                string location_second_reviewer = dtProjectConfig.Rows[0]["location_second_reviewer"].ToString();
                string location_third_reviewer = dtProjectConfig.Rows[0]["location_third_reviewer"].ToString();
                string font_pf_height_str = dtProjectConfig.Rows[0]["font_pf_height"].ToString();
                string font_pf_scale_str = dtProjectConfig.Rows[0]["font_pf_scale"].ToString();

                if (!string.IsNullOrEmpty(font_pf_height_str)) 
                {
                    font_pf_height = double.Parse(font_pf_height_str);
                }
                if (!string.IsNullOrEmpty(font_pf_scale_str))
                {
                    font_pf_scale = double.Parse(font_pf_scale_str);
                }
                

                if (location_designer == "1")
                {
                    string font_location_designer = dtProjectConfig.Rows[0]["font_location_designer"].ToString();
                    if (!string.IsNullOrEmpty(font_location_designer)) 
                    {
                        string[] parts = font_location_designer.Split(',');
                        string xString = parts[0].Split(':')[1];
                        string yString = parts[1].Split(':')[1];
                        font_location_designer_x = double.Parse(xString);
                        font_location_designer_y = double.Parse(yString);
                    }
                }
                if (location_reviewer == "1")
                {
                    string font_location_reviewer = dtProjectConfig.Rows[0]["font_location_reviewer"].ToString();
                    if (!string.IsNullOrEmpty(font_location_reviewer))
                    {
                        string[] parts = font_location_reviewer.Split(',');
                        string xString = parts[0].Split(':')[1];
                        string yString = parts[1].Split(':')[1];
                        font_location_reviewer_x = double.Parse(xString);
                        font_location_reviewer_y = double.Parse(yString);
                    }
                   
                }
                if (location_first_reviewer == "1")
                {
                    string font_location_first_reviewer = dtProjectConfig.Rows[0]["font_location_first_reviewer"].ToString();
                    if (!string.IsNullOrEmpty(font_location_first_reviewer))
                    {
                        string[] parts = font_location_first_reviewer.Split(',');
                        string xString = parts[0].Split(':')[1];
                        string yString = parts[1].Split(':')[1];
                        font_location_first_reviewer_x = double.Parse(xString);
                        font_location_first_reviewer_y = double.Parse(yString);
                    }
                }
                if (location_second_reviewer == "1")
                {
                    string font_location_second_reviewer = dtProjectConfig.Rows[0]["font_location_second_reviewer"].ToString();
                    if (!string.IsNullOrEmpty(font_location_second_reviewer))
                    {
                        string[] parts = font_location_second_reviewer.Split(',');
                        string xString = parts[0].Split(':')[1];
                        string yString = parts[1].Split(':')[1];
                        font_location_second_reviewer_x = double.Parse(xString);
                        font_location_second_reviewer_y = double.Parse(yString);
                    }
                }
                if (location_third_reviewer == "1")
                {
                    string font_location_third_reviewer = dtProjectConfig.Rows[0]["font_location_third_reviewer"].ToString();
                    if (!string.IsNullOrEmpty(font_location_third_reviewer))
                    {
                        string[] parts = font_location_third_reviewer.Split(',');
                        string xString = parts[0].Split(':')[1];
                        string yString = parts[1].Split(':')[1];
                        font_location_third_reviewer_x = double.Parse(xString);
                        font_location_third_reviewer_y = double.Parse(yString);
                    }
                }
                drawingConfigModel.location_designer = location_designer;
                drawingConfigModel.location_reviewer = location_reviewer;
                drawingConfigModel.location_first_reviewer = location_first_reviewer;
                drawingConfigModel.location_second_reviewer = location_second_reviewer;
                drawingConfigModel.location_third_reviewer = location_third_reviewer;

                string font_location_drawingname = dtProjectConfig.Rows[0]["font_location_drawingname"].ToString();
                string[] partname = font_location_drawingname.Split(',');
                xStringname = partname[0].Split(':')[1];
                yStringname = partname[1].Split(':')[1];
             

                string font_location_drawing_no = dtProjectConfig.Rows[0]["font_location_drawing_no"].ToString();
                string[] partno = font_location_drawing_no.Split(',');
                 xStringno = partno[0].Split(':')[1];
                 yStringno = partno[1].Split(':')[1];
              
            }
            drawingConfigModel.font_location_designer_x = font_location_designer_x;
            drawingConfigModel.font_location_designer_y = font_location_designer_y;
            drawingConfigModel.font_location_reviewer_x = font_location_reviewer_x;
            drawingConfigModel.font_location_reviewer_y = font_location_reviewer_y;
            drawingConfigModel.font_location_first_reviewer_x = font_location_first_reviewer_x;
            drawingConfigModel.font_location_first_reviewer_y = font_location_first_reviewer_y;
            drawingConfigModel.font_location_second_reviewer_x = font_location_second_reviewer_x;
            drawingConfigModel.font_location_second_reviewer_y = font_location_second_reviewer_y;
            drawingConfigModel.font_location_third_reviewer_x = font_location_third_reviewer_x;
            drawingConfigModel.font_location_third_reviewer_y = font_location_third_reviewer_y;
            drawingConfigModel.font_location_page_x = font_location_page_x;
            drawingConfigModel.font_location_page_y = font_location_page_y;
            drawingConfigModel.font_location_page_sum_x = font_location_page_sum_x;
            drawingConfigModel.font_location_page_sum_y = font_location_page_sum_y;
            drawingConfigModel.font_pf_height = font_pf_height;
            drawingConfigModel.font_pf_scale = font_pf_scale;
            drawingConfigModel.font_location_drawingname_x = double.Parse(xStringname);
            drawingConfigModel.font_location_drawingname_y = double.Parse(yStringname);
            drawingConfigModel.font_location_drawing_no_x = double.Parse(xStringno);
            drawingConfigModel.font_location_drawing_no_y = double.Parse(yStringno);

            return drawingConfigModel;
        }

        /// <summary>
        /// 图框文字绘制
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tKPointPile"></param>
        /// <param name="i"></param>
        /// <param name="drawingConfigModel"></param>
        /// <param name="ex"></param>
        /// <param name="page"></param>
        /// <param name="count"></param>
        public static void AddPaperDBText(Database db, Point2d tKPointPile,int i, DrawingConfigModel drawingConfigModel, ref Extents2d ex,int page,int count) 
        {
            if (drawingConfigModel.location_designer == "1") 
            {
                TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_designer_x, drawingConfigModel.font_location_designer_y), ref ex, drawingConfigModel.designer, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

            }
            if (drawingConfigModel.location_reviewer == "1")
            {
                TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_reviewer_x, drawingConfigModel.font_location_reviewer_y), ref ex, drawingConfigModel.reviewer, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

            }
            if (drawingConfigModel.location_first_reviewer == "1")
            {
                TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_first_reviewer_x, drawingConfigModel.font_location_first_reviewer_y), ref ex, drawingConfigModel.first_reviewer, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

            }
            if (drawingConfigModel.location_second_reviewer == "1")
            {
                TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_second_reviewer_x, drawingConfigModel.font_location_second_reviewer_y), ref ex, drawingConfigModel.second_reviewer, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

            }
            if (drawingConfigModel.location_third_reviewer == "1")
            {
                TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_third_reviewer_x, drawingConfigModel.font_location_third_reviewer_y), ref ex, drawingConfigModel.third_reviewer, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

            }
            TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_drawingname_x, drawingConfigModel.font_location_drawingname_y), ref ex, drawingConfigModel.name, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);
            TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_drawing_no_x, drawingConfigModel.font_location_drawing_no_y), ref ex, drawingConfigModel.drawing_no, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

            //TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_page_x, drawingConfigModel.font_location_page_y), ref ex, (startP + index).ToString(), 1, 3.5, "SRB.HT", 0, TextHorizontalMode.TextCenter);
            TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_page_x, drawingConfigModel.font_location_page_y), ref ex, page.ToString(), drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

            TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_page_sum_x, drawingConfigModel.font_location_page_sum_y), ref ex, count.ToString(), drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);
        }

        public static void AddPaperDBText(Database db, Point2d tKPointPile,  DrawingConfigModel drawingConfigModel, ref Extents2d ex,  int count,int startPage=1)
        {
            for (int i = 0; i < count; i++)
            {
                int page = startPage>1? startPage: i + 1;
                if (drawingConfigModel.location_designer == "1")
                {
                    TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_designer_x, drawingConfigModel.font_location_designer_y), ref ex, drawingConfigModel.designer, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

                }
                if (drawingConfigModel.location_reviewer == "1")
                {
                    TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_reviewer_x, drawingConfigModel.font_location_reviewer_y), ref ex, drawingConfigModel.reviewer, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

                }
                if (drawingConfigModel.location_first_reviewer == "1")
                {
                    TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_first_reviewer_x, drawingConfigModel.font_location_first_reviewer_y), ref ex, drawingConfigModel.first_reviewer, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

                }
                if (drawingConfigModel.location_second_reviewer == "1")
                {
                    TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_second_reviewer_x, drawingConfigModel.font_location_second_reviewer_y), ref ex, drawingConfigModel.second_reviewer, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

                }
                if (drawingConfigModel.location_third_reviewer == "1")
                {
                    TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_third_reviewer_x, drawingConfigModel.font_location_third_reviewer_y), ref ex, drawingConfigModel.third_reviewer, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

                }
                TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_drawingname_x, drawingConfigModel.font_location_drawingname_y), ref ex, drawingConfigModel.name, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);
                TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_drawing_no_x, drawingConfigModel.font_location_drawing_no_y), ref ex, drawingConfigModel.drawing_no, drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

                //TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_page_x, drawingConfigModel.font_location_page_y), ref ex, (startP + index).ToString(), 1, 3.5, "SRB.HT", 0, TextHorizontalMode.TextCenter);
                TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_page_x, drawingConfigModel.font_location_page_y), ref ex, (page+i).ToString(), drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);

                TextPloter.AddPaperDBText(db, tKPointPile.C3D(420 * i + drawingConfigModel.font_location_page_sum_x, drawingConfigModel.font_location_page_sum_y), ref ex, count.ToString(), drawingConfigModel.font_pf_scale, drawingConfigModel.font_pf_height, "SRB.HT", 0, TextHorizontalMode.TextCenter);
            }
        }
    }
}
